package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {
    @Autowired  //规格组
            SpecGroupService specGroupService;
    @Autowired  //规格参数
            SpecParamService specParamService;

    //根据商品分类id查询规格组
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroup( @RequestParam("id") Long id) {
        List<SpecGroup> groupList = specGroupService.query().eq("category_id", id).list();
        return ResponseEntity.ok(SpecGroupDTO.convertEntityList(groupList));
    }

    //查询规格参数集合
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) boolean searching
    ) {
        List<SpecParamDTO> list = specParamService.querySpecParams(categoryId, groupId, searching);
        return ResponseEntity.ok(list);
    }

    //新增规格组
    @PostMapping("/group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO groupDTO) {
        specGroupService.save(groupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //修改规格组
    @PutMapping("/group")
    public ResponseEntity<Void> putGroup(@RequestBody SpecGroupDTO groupDTO){
        specGroupService.updateById(groupDTO.toEntity(SpecGroup.class));
        return  ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //新增规格参数
    @PostMapping("/param")
    public ResponseEntity<Void> PostSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.save(specParamDTO.toEntity(SpecParam.class));
        return  ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //修改规格参数
    @PutMapping("/param")
    public ResponseEntity<Void> putSpecParam(@RequestBody SpecParamDTO  specParamDTO){
        specParamService.updateById(specParamDTO.toEntity(SpecParam.class));
        return  ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //查询规格组及规格参数    ????steam问题
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> getSpecGroupDTOByid(@PathVariable("id")Long id){
        List<SpecGroupDTO> list= specGroupService.getSpecGroupDTOByid(id);
        return  ResponseEntity.ok(list);
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id")Long cid){
        return ResponseEntity.ok(this.specGroupService.querySpecList(cid));
    }

}